import { Component, OnInit, Input } from '@angular/core';

import { NzDrawerRef, NzDrawerService, NzModalService } from 'ng-zorro-antd';

import { LibraryService } from 'src/app/core/service/library.service';
import { LicenseService } from 'src/app/core/service/license.service';
import { CveService } from 'src/app/core/service/cve.service';
import { ContainerService } from 'src/app/core/service/container.service';
import { BaselineListComponent } from './baseline-list/baseline-list.component';
import { AddRelevanceComponent } from '../../data-center-modal/add-relevance/add-relevance.component';

@Component({
  selector: 'app-detail-table',
  templateUrl: './detail-table.component.html',
  styleUrls: ['./detail-table.component.scss']
})
export class DetailTableComponent implements OnInit {
  @Input() title: string;
  theads = [];
  data = [];

  pageIndex = 1;
  pageSize = 10;
  loading = true;
  total = 0;

  constructor(
    private libraryService: LibraryService,
    private licenseService: LicenseService,
    private cveService: CveService,
    private containerService: ContainerService,
    private drawerService: NzDrawerService,
    private modalService: NzModalService,
  ) { }

  /**获取组件列表 */
  getTableData(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;

    if (this.title === '组件') {
      this.libraryService
        .getThirdPartyLibList(this.pageIndex, this.pageSize)
        .subscribe((LibListData) => {
          this.loading = false;
          this.total = LibListData.total;
          this.data = LibListData.data;
          console.log('第三方库', LibListData);
        });
    }
    if (this.title === 'License') {
      this.licenseService
        .getLicenseList(this.pageIndex, this.pageSize)
        .subscribe((licenseListData) => {
          this.loading = false;
          this.total = licenseListData.total;
          this.data = licenseListData.data;
          console.log('license', licenseListData);
        });
    }
    if (this.title === 'cve') {
      this.cveService
        .getCveList(this.pageIndex, this.pageSize)
        .subscribe((cveListData) => {
          this.loading = false;
          this.total = cveListData.total;
          this.data = cveListData.data;
          console.log('漏洞', cveListData);
        });
    }
    if (this.title === 'cnnvd') {
      this.cveService
        .getCnnvdList(this.pageIndex, this.pageSize)
        .subscribe((cnnvdListData) => {
          this.loading = false;
          this.total = cnnvdListData.total;
          this.data = cnnvdListData.data;
          console.log('漏洞', cnnvdListData);
        });
    }
    if (this.title === '容器基线') {
      this.containerService
        .getContainerList(this.pageIndex, this.pageSize)
        .subscribe((containerListData) => {
          this.loading = false;
          this.total = containerListData.total;
          this.data = containerListData.data;
          console.log('容器基线', containerListData);
        });
    }
  }

  openComponent(containerUuid: string): void {
    const drawerRef = this.drawerService.create<BaselineListComponent, { containerUuid: string }, string>({
      nzTitle: '基线详情',
      nzContent: BaselineListComponent,
      nzWidth: 1000,
      nzContentParams: {
        containerUuid: containerUuid
      }
    });

    drawerRef.afterOpen.subscribe(() => {
      console.log('Drawer(Component) open');
    });

    drawerRef.afterClose.subscribe(data => {
      console.log(data);
      if (typeof data === 'string') {
        // this.value = data;
      }
    });
  }

  createComponentModal(name): void {
    console.log('===name===', name);
    const modal = this.modalService.create({
      nzTitle: '添加关联组件',
      nzContent: AddRelevanceComponent,
      nzComponentParams: {
        name: name
      },
      nzFooter: null
    });
  }

  ngOnInit() {
    if (this.title === '组件') {
      this.theads = ['组件名称', '分数', '发布日期', '最近发布日期', '最新版本', '许可ID'];
    }
    if (this.title === 'License') {
      this.theads = ['License名称', '是否开源', '是否过期', '参考地址', '参考编号', '详细地址'];
    }
    if (this.title === 'cve') {
      this.theads = ['CVE名称', '分数', '可用性影响', '访问矢量', '访问复杂性', '保密性影响', '完整性影响', '关联组件'];
    }
    if (this.title === 'cnnvd') {
      this.theads = ['cnnvd名称', 'cnnvd编号', '发布时间', '来源', '严重性', '漏洞类型', '访问矢量', '描述', '对应的cve编号', '漏洞解决方案'];
    }
    if (this.title === '容器基线') {
      this.theads = ['容器名称', '版本号', '数据来源', 'CVE数量', 'CNNVD数量', '安全基线'];
    }
    this.getTableData();

  }

}
